from backend.backend import Backend
from workflow.task import Task
from workflow.result import Result 
from workflow.flowData import Status

class RetailBackend(Backend):
    
    def __init__(self):       
        Backend.__init__(self, "Restock2")

    def wire(self):
        self.register_result_listener("Manager", "Examine", self.Exam_Complete)
        self.register_result_listener("Employee", "Stockroom", self.Stockroom)
        self.register_result_listener("Employee", "PlaceOnFloor", self.PlaceOnFloor)
        self.register_result_listener("Manager", "Replacement", self.Replacement)
        self.register_result_listener("Employee", "FetchNew", self.FetchNew)
        self.register_result_listener("Employee", "PlaceNew", self.PlaceNew)

    
    def Exam_Complete(self, results):
        for result in results:
            if result.data['stockneeded'] == "TRUE":
                task = Task.construct_from_result(result, "Employee", "Stockroom");
                self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);
    
    def Stockroom(self, results):
        for result in results:
            if result.data['itemavailable'] == "TRUE":
                task = Task.construct_from_result(result, "Employee", "PlaceOnFloor");
                self.workflow.add(task);
            elif result.data['itemavailable'] == "FALSE":
               task = Task.construct_from_result(result, "Manager", "Replacement");
               self.workflow.add(task); 
            self.workflow.update_status(result, Status.COMPLETE);
    
    def PlaceOnFloor(self, results):
        for result in results: 
            if result.data['placementcomplete'] == "FALSE":
                task = Task.construct_from_result(result, "Employee", "PlaceOnFloor");
                self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE)

    def PlaceNew(self, results):
        for result in results: 
            if result.data['placementcompletenew'] == "FALSE":
                task = Task.construct_from_result(result, "Employee", "PlaceNew");
                self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE)
    
    def Replacement(self, results):
        for result in results: 
            task =  Task.construct_from_result(result,"Employee", "FetchNew")
            self.workflow.add(task)
            self.workflow.update_status(result, Status.COMPLETE)
            
    def FetchNew(self, results):
        for result in results:
            if result.data['itemfetched'] == "FALSE":
                task = Task.construct_from_result(result, "Employee", "FetchNew");
                self.workflow.add(task);
            elif result.data['itemfetched'] == "TRUE":
                task = Task.construct_from_result(result, "Employee", "PlaceNew");
                self.workflow.add(task); 
            self.workflow.update_status(result, Status.COMPLETE) 
            
if __name__ == '__main__':
    backend = RetailBackend()       